package com.zhuxin.medical.mvc.service;

import com.dbHelper.DbTemplate;
import com.zhuxin.medical.model.po.CommWxUser;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by lzb on 2018/9/18.
 */
@Service
@Transactional
public class CommWxSerivce {

    private static final Logger log = LoggerFactory.getLogger(CommWxSerivce.class);
    @Autowired
    private DbTemplate mysqlDao;

    @Autowired
    private DbTemplate oracleDao;


    /**
     * 根据OpenID找人
     * @param openid
     * @return
     */
    public CommWxUser findUserById(String openid){
        CommWxUser user=new CommWxUser();
        user.setOpenid(openid);
        return this.mysqlDao.queryVoByKey(user);
    }

    public void saveUser(CommWxUser user){
        this.mysqlDao.save(user);
    }

    /**
     * 人员信息入库
     * @param result
     */
    public void saveUserinfo(String result) {
        CommWxUser po = new CommWxUser();
        JSONObject json=new JSONObject(result);
        String openid = json.getString("openid");
        po.setOpenid(openid);
        po.setNickname(json.getString("nickname"));
        po.setUsername("");
        po.setUsertype(0);//默认为0  1为管理者
        try {
            this.mysqlDao.save(po);
        } catch (Exception e) {
            log.info("用户已经存在,不再重复新增");
            //e.printStackTrace();
        }
        log.info("人员信息[{}]入库完成",po.toString());
    }
}
